
import java.util.logging.Level;
import java.util.logging.Logger;

import com.jme.app.AbstractGame;
import com.jme.app.BaseGame;
import com.jme.input.KeyInput;
import com.jme.input.MouseInput;
import com.jme.input.joystick.JoystickInput;
import com.jme.system.DisplaySystem;
import com.jme.system.JmeException;
import com.jme.util.Timer;
import com.jmex.game.state.GameState;
import com.jmex.game.state.GameStateManager;

public class TestGameStateSystem extends BaseGame {
    private static final Logger logger = Logger
            .getLogger(TestGameStateSystem.class.getName());
    private static AbstractGame instance;
    private Timer timer;
    private float tpf;
    public static float walkSpeed=4f;
    public static float rotateSpeed=100f;
    
    protected final void update(float interpolation) {
        timer.update();
        tpf = timer.getTimePerFrame();
        GameStateManager.getInstance().update(tpf);
    }
    
    protected final void render(float interpolation) {
        display.getRenderer().clearBuffers();
        GameStateManager.getInstance().render(tpf);
    }
    
    protected final void initSystem() {
        try {
            display = DisplaySystem.getDisplaySystem(settings.getRenderer());
            display.createWindow(
                    settings.getWidth(),
                    settings.getHeight(),
                    settings.getDepth(),
                    settings.getFrequency(),
                    settings.isFullscreen());   
        }
        catch (JmeException e) {
            logger.log(Level.SEVERE, "Could not create displaySystem", e);
            System.exit(1);
        }
        timer = Timer.getTimer();        
    }
    
    protected final void initGame() {       
        instance = this;
        display.setTitle("Test Game State System");
        GameStateManager.create();
        GameState menu = new MenuState("menu");
        menu.setActive(true);
        GameStateManager.getInstance().attachChild(menu);
    }
    
    protected void reinit() { }
    
    protected void cleanup() {
        logger.info("Cleaning up resources.");
        GameStateManager.getInstance().cleanup();        
        KeyInput.destroyIfInitalized();
        MouseInput.destroyIfInitalized();
        JoystickInput.destroyIfInitalized();
    }
    
    public static void main(String[] args) {
        TestGameStateSystem app = new TestGameStateSystem();
        //app.setConfigShowMode(ConfigShowMode.AlwaysShow);
        app.setConfigShowMode(ConfigShowMode.NeverShow);
        MouseInput.get().setCursorVisible(true);
        app.start();
    }
    
    public static void exit() {
        instance.finish();
    }    
}
